package com.woniuxy.security.realm;

import com.woniuxy.security.util.Audience;
import com.woniuxy.security.util.JwtToken;
import com.woniuxy.security.util.JwtUtil;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;

public class JwtRealm extends AuthorizingRealm{
    @Resource
    private Audience audience;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }


    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String jwt = authenticationToken.getCredentials().toString();
        if (JwtUtil.parseJwt(jwt,audience.getBase64Secret())){
            return new SimpleAuthenticationInfo(jwt,jwt,this.getName());
        }
        return null;
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JwtToken;
    }
}
